package ClientCommTests;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import org.junit.Test;

import client.TelnetClient;
import clientCommunicationHandler.ClientServerMsgHandler;
import clientCommunicationHandler.ReadNewMsg;
import clientCommunicationHandler.UserInterfaceMsgHandler;

public class UserInterfaceMsgHandlerTest {
	private final String serverDelimiter = (char)1+"";
	InputStream tempInPutStram;
	public void setUp(){
		tempInPutStram = System.in;
	}
	@Test
	public void testLoginMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. login\n".split(" ");
		String streamInput = "test\ntest\n";
		 System.setIn(new ByteArrayInputStream(streamInput.getBytes()));
		 InputStreamReader t = new InputStreamReader(System.in);
		 TelnetClient.in = new BufferedReader(t);
		 assertEquals("login"+serverDelimiter+"test"+serverDelimiter+"test"+serverDelimiter,parser.initParse(initParseInput));
		
	}
	@Test
	public void testviewForumMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. view_Forums\n".split(" ");
		 assertEquals("viewForum"+serverDelimiter,parser.initParse(initParseInput));
		
	}
	@Test
	public void testRegisterMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. Register\n".split(" ");
		String streamInput = "test\ntest\n";
		 System.setIn(new ByteArrayInputStream(streamInput.getBytes()));
		 InputStreamReader t = new InputStreamReader(System.in);
		 TelnetClient.in = new BufferedReader(t);
		 assertEquals("register"+serverDelimiter+"test"+serverDelimiter+"test"+serverDelimiter,parser.initParse(initParseInput));
		
	}
	
	@Test
	public void testViewSubForumMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. viewThreads 1:sport\n".split(" ");
		 assertEquals("viewThread"+serverDelimiter+"1"+serverDelimiter,parser.initParse(initParseInput));
		
	}
	@Test
	public void testLogoutMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. logout\n".split(" ");
		 assertEquals("logout"+serverDelimiter+"guest"+serverDelimiter,parser.initParse(initParseInput));	
	}
	@Test
	public void testAddSubForumt(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. add-new-SubForum\n".split(" ");
		String streamInput = "testSubForumName\ntestSubForumName\n";
		 System.setIn(new ByteArrayInputStream(streamInput.getBytes()));
		 InputStreamReader t = new InputStreamReader(System.in);
		 TelnetClient.in = new BufferedReader(t);
		 assertEquals("addSubForum"+serverDelimiter+"guest"+serverDelimiter+"testSubForumName"+serverDelimiter+"\n\0\n",parser.initParse(initParseInput));	
	}
	@Test
	public void testAddModerator(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. test".split(" ");
		ClientServerMsgHandler.subForumId ="1";
		 assertEquals("addModerator"+serverDelimiter+"guest"+serverDelimiter+"test"+serverDelimiter+"1"+serverDelimiter+"\n\0\nviewForum"+serverDelimiter,parser.initParse(initParseInput));	
	}
	@Test
	public void testdeleteMsg(){
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. removePost 3:p".split(" ");
		TelnetClient.userPosition = new Vector<String>();
		TelnetClient.userPosition.add("1");
		TelnetClient.userPosition.add("2");
		TelnetClient.userName = "test";
		String expected = "deleteMsg"+serverDelimiter+"test"+serverDelimiter+"1"+serverDelimiter+"2"+serverDelimiter+"3"+serverDelimiter;
		String goingToSendToServer = parser.initParse(initParseInput);  
		 assertEquals(expected,goingToSendToServer);	
	}
	@Test
	public void testEditeMsg(){
		String streamInput = "newSubject\nnewBody\n";
		 System.setIn(new ByteArrayInputStream(streamInput.getBytes()));
		 InputStreamReader t = new InputStreamReader(System.in);
		 TelnetClient.in = new BufferedReader(t);
		UserInterfaceMsgHandler parser = new UserInterfaceMsgHandler();
		String [] initParseInput = "1. editPost 3:p".split(" ");
		TelnetClient.userPosition = new Vector<String>();
		TelnetClient.userPosition.add("1");
		TelnetClient.userPosition.add("2");
		TelnetClient.userName = "test";
		String expected = "editPost"+serverDelimiter+"test"+serverDelimiter+"1"+serverDelimiter+"2"+serverDelimiter+"3"+serverDelimiter+"newSubject"+(char)2+"newBody"+serverDelimiter+"\n\0\n";
		String goingToSendToServer = parser.initParse(initParseInput);  
		 assertEquals(expected,goingToSendToServer);	
	}
	
	
			
//	
//	
//	@Test
//	public void  handleNewPostMsgTest() {
//		String postMsg="";
//		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
//		ReadNewMsg tMsg = new ReadNewMsg();
//		String msg = tMsg.readMsgFromUser();
//		
//		String temp = "addPost\n"+TelnetClient.userName+"\n"+msg+"\n"+Integer.toString(TelnetClient.SubForumId)+"\n"
//		+Integer.toString(TelnetClient.threadId)+"\n"+"11"+"\n\0\n"+
//		handler.refreshLastViewedThread();
//		
//		
//		postMsg=handler.handleNewPostMsg("11");
//		
//		assertEquals(temp, postMsg);
//	}
//	
//	
//	@Test
//	public void BuildViewSubForumMsgTest() {
//		String subForumId;
//		String temp;
//		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
//		TelnetClient.SubForumId = Integer.parseInt("111");
//		temp =  "viewSubForum\n" + "111"+"\n\0\n";
//		subForumId = handler.BuildViewSubForumMsg("111");
//		assertEquals(temp, subForumId);
//	}
//	
//	public String refreshLastViewedThread(){
//		return "viewPost\n"+TelnetClient.SubForumId+"\n"+TelnetClient.threadId +"\n\0\n";
//	}
//	
//	@Test
//	public void refreshLastViewedThreadTest(){
//		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
//		String tmp = handler.refreshLastViewedThread();
//		String test =  "viewPost\n"+TelnetClient.SubForumId+"\n"+TelnetClient.threadId +"\n\0\n";
//		assertEquals(tmp, test);
//	}
//	
//	
//	@Test
//	public void refreshViewTest(){
//		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
//		String tmp = handler.refreshView();
//		String test = "";
//		if(TelnetClient.threadId > 0){
//			test = refreshLastViewedThread();
//		}
//		else{
//			if(TelnetClient.SubForumId > 0){
//				test = handler.refreshLastViewedsubForum();
//			}
//		}
//		if(test.equals(""))
//			TelnetClient.transmitToServer = false;
//	
//		assertEquals(tmp, test);
//	}
//	
//	
//	@Test
//	public void handleBackMsgTest(){
//		UserInterfaceMsgHandler handler = new UserInterfaceMsgHandler();
//		String temp = "";
//		if(TelnetClient.postId > 0){
//			temp  = handler.BuildViewThreadMsg(Integer.toString(TelnetClient.threadId));
//		}
//		else{
//			if(TelnetClient.SubForumId > 0){
//				handler.BuildViewSubForumMsg(Integer.toString(TelnetClient.SubForumId));
//			}
//			else{
//				TelnetClient.menu = TelnetClient.welcom_page;
//				TelnetClient.transmitToServer = false;
//			}
//		}
//		
//		String test = handler.handleBackMsg();
//		assertEquals(temp, test);
//		
//	}
//
//	
//	
//	

	
	
	
	

}
